//
//  LSTCycleImages.m
//  06-轮播图
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "LSTCycleImages.h"

@interface LSTCycleImages ()<UIScrollViewDelegate>
@property (nonnull, nonatomic, strong)UIPageControl *page;
@property (nonnull, nonatomic, strong)UIScrollView *scrollView;
/** 定时器时间间隔 */
@property(nonatomic, assign)CGFloat timeInterval;
/** 定时器对象 */
@property(nonatomic, strong)NSTimer *cycleTimer;
/** 当前item的下标 */
@property(nonatomic, assign)NSInteger itemIndex;
/** 当前section下标 */
@property(nonatomic, assign)NSInteger currentSection;
@end
@implementation LSTCycleImages
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    _page.currentPage = scrollView.contentOffset.x / self.bounds.size.width;
    _page.currentPage = page - 1;
    if (page == 0) {
        _scrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imagesArr.count - 2), 0);
        _page.currentPage = _page.numberOfPages - 3;
    } else if (page == _imagesArr.count - 1) {
        _scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _page.currentPage = 0;
    }
}
#pragma mark - 初始化 initWithFrame
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createScrollView];
        [self createImagePage:frame];
        [self addTimer];
    }
    return self;
}
#pragma mark - scrollView
- (void)createScrollView {
    // 子控件初始化时, frame不要与初始化方法的frame一样
    // 推荐使用self.Bounds/cgrectMake(0,0,frame.size.width, frame.size.height)
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    [self addSubview:_scrollView];
}
#pragma make - 后期需要添加page的位置改变, 这里是默认改变
- (void) createImagePage:(CGRect) frame {
    _page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, 20)];
    _page.numberOfPages = 5;
    
    _page.center = CGPointMake(frame.size.width / 2, frame.size.height - 10);
    [self addSubview:_page];
}

// 问题:如何通过page进行轮播
- (void)clickPage:(UIPageControl *)pageControl {
    [_scrollView setContentOffset:CGPointMake((pageControl.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
}
#pragma mark - ImageArray
// 自定义方法
- (void)setImagesWithArray:(NSArray *)array {
    // 根据传过来的数组, 进行处理, 生成图片数组
    NSArray * imagesArray = [self handleArray:array];
    // 逻辑判断时, 需要用到数组内容
    self.imagesArr = [NSArray arrayWithArray:imagesArray];
    // 3 根据生成对应的ImageView
    [self creatAllImageViewsWithImagesArray:imagesArray];
    _page.numberOfPages = array.count;
}
- (NSArray *)handleArray:(NSArray *) array {
    id firstObject = [array firstObject];
    // 最终的图片数组
    NSMutableArray *imagemutArray = [NSMutableArray array];
    // 判断对象是不是某种类型 isKindOfClass
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 把字符串数组 转成 图片数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imagemutArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        
        [imagemutArray setArray:array];
    }
    // 获取到第一张图
    UIImage *firstImage = [imagemutArray firstObject];
    // 获取到最后一张图
    UIImage *lastImage = [imagemutArray lastObject];
    // 下标为0的位置放最后一张
    [imagemutArray insertObject:lastImage atIndex:0];
    // 在最后添加一张图
    [imagemutArray addObject:firstImage];
    return  [imagemutArray copy];
}
- (void)creatAllImageViewsWithImagesArray:(NSArray *)imagesArray {
    // 使用数组创建ImageView
    for (int i = 0; i < imagesArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width , 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imagesArray[i];
        [_scrollView addSubview:imageView];
        [_page addTarget:self action:@selector(clickPage:) forControlEvents:UIControlEventValueChanged];
    }
    _scrollView.contentSize = CGSizeMake(imagesArray.count * self.bounds.size.width, 0);
    // 因为添加了最后一张图, 要让轮播图看起来在第一张
    _scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    //_page.numberOfPages = array.count;
}
#pragma mark -- 添加定时器:
- (void)addTimer
{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:self.timeInterval target:self selector:@selector(changePageByTime) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.cycleTimer = timer;
}
#pragma mark -- 移除定时器:
- (void)removeTimer
{
    [self.cycleTimer invalidate];
    self.cycleTimer = nil;
}
#pragma mark -- 定时器调用的方法:
- (void)changePageByTime
{
    if (_itemIndex == self.imagesArr.count) {
        _itemIndex = 0;
        _currentSection++;
    }
    if (self.imagesArr.count > 0) {
        //偏移到最新位置(下一张)
       
        _itemIndex++;
    }
}

#pragma mark -- 手指开始滑动时定时器失效:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (_timeInterval > 0) {
        [self removeTimer];
    }
}
#pragma mark -- 手指离开定时器开启:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (_timeInterval > 0) {
        [self addTimer];
    }
}

@end
